﻿using System;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using PhotoBlog.Core.Models;
using PhotoBlog.Core.Models.Requests;
using PhotoBlog.Models;
using PhotoBlog.Models.Users;

namespace PhotoBlog.Controllers
{
    public class RequestController : Controller
    {
        private readonly IRequestRepository requestRepository;
        private readonly IUserRepository userRepository;
        public RequestController(IRequestRepository requestRepository, IUserRepository userRepository)
        {
            this.requestRepository = requestRepository;
            this.userRepository = userRepository;
        }

        public ActionResult New()
        {
            var model = new RequestM();
            return View("Request", model);
        }

        public ActionResult Success()
        {
            return View("Success");
        }

        [HttpPost]
        public ActionResult New(RequestM requestM)
        {
            if (string.IsNullOrWhiteSpace(requestM.FirstName))
            {
                ModelState.AddModelError("FirstName", "Заполните имя");
            }
            if (string.IsNullOrWhiteSpace(requestM.LastName))
            {
                ModelState.AddModelError("LastName", "Заполните фамилию");
            }
            if (string.IsNullOrWhiteSpace(requestM.ContactInfo))
            {
                ModelState.AddModelError("ContactInfo", "Заполните контактную информацию");
            }
            if (ModelState.IsValid)
            {
                var request = new Request
                {
                    Comment = requestM.Comment ?? "",
                    ContactInfo = requestM.ContactInfo ?? "",
                    FirstName = requestM.FirstName,
                    LastName = requestM.LastName,
                    CountPhotos = requestM.CountPhotos,
                    CreateForm = requestM.CreateForm,
                    Makeup = requestM.Makeup,
                    StudioStyle = requestM.StudioStyle,
                    Date = DateTime.Now
                };
                requestRepository.Add(request);
                return RedirectToAction("Success");
            }
            return View("Request");
        }

        public ActionResult Index()
        {
            var user = userRepository.GetUserByLogin(User.Identity.Name);
            if (user == null) return RedirectToAction("Login", "Account");
            if (!user.IsAdmin && !user.IsModerator) return RedirectToAction("Login", "Account");

            var model = requestRepository.GetRequests();
            return View(model.ToList());
        }

        public ActionResult Delete(int? id)
        {
            var isAdmin = userRepository.UserIsAdmin(User.Identity.Name);
            if (!isAdmin)
            {
                return View("_Layout");
            }
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var request = requestRepository.Find(id.Value);
            if (request == null)
            {
                return HttpNotFound();
            }
            return View(request);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var isAdmin = userRepository.UserIsAdmin(User.Identity.Name);
            if (!isAdmin)
            {
                return View("_Layout");
            }
            requestRepository.Delete(id);
            return RedirectToAction("Index");
        }
    }
}
